package com.lancer.platform.pay.rest.param;

import com.lancer.platform.pay.utils.ListUtils;
import com.lancer.platform.pay.utils.ListUtilsHook;
import com.lancer.platform.pay.utils.ListUtilsHookUp;

import javax.ws.rs.core.Form;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class FormParam {

    private final ConcurrentMap<String, List<String>> formParams;

    private FormParam() {
        this.formParams = new ConcurrentHashMap<>();
    }

    public static FormParam build() {
        return new FormParam();
    }

    public FormParam append(String formParamName, String... formParams) {
        Objects.requireNonNull(formParamName, "FormParamName is null.");
        Objects.requireNonNull(formParams, "FormParams is null.");
        List<String> toAppendValues = ListUtils.filter(Arrays.asList(formParams), new ListUtilsHook<String>() {
            @Override
            public boolean test(String str) {
                return str != null;
            }
        });
        List<String> formValues = this.formParams.putIfAbsent(formParamName, toAppendValues);

        if (formValues != null) {
            formValues.addAll(toAppendValues);
        }

        return this;
    }

    public FormParam append(String formParamName, List<String> formParams) {
        Objects.requireNonNull(formParamName, "FormParamName is null.");
        Objects.requireNonNull(formParams, "FormParams is null.");
        List<String> toAppendValues = ListUtils.filter(formParams, new ListUtilsHook<String>() {
            @Override
            public boolean test(String str) {
                return str != null;
            }
        });
        List<String> formValues = this.formParams.putIfAbsent(formParamName, toAppendValues);
        if (formValues != null) {
            formValues.addAll(toAppendValues);
        }

        return this;
    }

    public Form toForm() {
        Form form = new Form();
        for (String formParamName : this.formParams.keySet()) {
            List<String> formParamValues = this.formParams.get(formParamName);
            for (String formParamValue : formParamValues) {
                form.param(formParamName, formParamValue);
            }
        }

        return form;
    }

    @Override
    public String toString() {
//        return this.formParams.entrySet().stream().filter(e -> e.getKey() != null && e.getValue() != null)
//                .map(
//                        e -> e.getValue().stream().filter(v -> v != null)
//                        .map(v -> e.getKey() + "=" + v)
//                .collect(Collectors.joining("&")))
//                .collect(Collectors.joining("&", "{FormParam: ", "}"));
        List<String> strList = new ArrayList<>();
        for (final Map.Entry<String, List<String>> entry : this.formParams.entrySet()) {
            if (entry.getKey() == null || entry.getValue() == null) {
                continue;
            }
            List<String> r = ListUtils.filter(entry.getValue(), new ListUtilsHookUp<String>() {
                String key = entry.getKey();
                @Override
                public boolean test(String s) {
                    return s != null;
                }

                @Override
                public String doing(String v) {
                    return  key + "=" + v;
                }

            });
            strList.add(ListUtils.joining(r, "&"));
        }
        return ListUtils.joining(strList, "&", "{FormParam:","}");
    }
}
